﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace KpiConfigProject.TypeEditor.Controls
{
    public partial class RichTextBoxExtend : RichTextBox
    {
        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        private static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpKeyState, int[] lpChar, int uFlags);

        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        private static extern int GetKeyboardState(byte[] pbKeyState);

        ListBox selectBox;

        private char inputChar;
        /// <summary>
        /// 
        /// </summary>
        public RichTextBoxExtend()
        {
            InitializeComponent();
            selectBox = new ListBox();
            selectBox.MouseDoubleClick += new MouseEventHandler(selectBox_MouseDoubleClick);
            selectBox.KeyDown += new KeyEventHandler(selectBox_KeyDown);
            selectBox.Width = 100;
            selectBox.Items.AddRange(new object[] { "12", "13", "14" });
            selectBox.Hide();
            this.Controls.Add(selectBox);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void selectBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                SelectBoxSelectItem();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void selectBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SelectBoxSelectItem();
        }

        private void SelectBoxSelectItem()
        {
            if (selectBox.SelectedIndex <= -1)
                return;
            int firstIndex = this.SelectionStart - 1;
            this.AppendText(selectBox.SelectedItem.ToString());
            if (inputChar == '[')
                this.AppendText("]");
            else if (inputChar == '{')
                this.AppendText("}");
            selectBox.Hide();
            selectBox.SelectedIndex = -1;
            int endIndex = this.SelectionStart;
            //MySelect(this, firstIndex, endIndex, Color.DarkOrange, true);
            this.Focus();
        }


        //设定颜色
        public static void MySelect(System.Windows.Forms.RichTextBox tb, int firstIndex, int endIndex, Color c, bool font)
        {
            tb.Select(firstIndex, endIndex);
            tb.SelectionColor = c;
            //是否改变字体
            if (font)
                tb.SelectionFont = new Font("宋体", 12, (FontStyle.Bold));
            else
                tb.SelectionFont = new Font("宋体", 12, (FontStyle.Regular));
            //以下是把光标放到原来位置，并把光标后输入的文字重置
            tb.Select(endIndex, 0);
            tb.SelectionFont = new Font("宋体", 12, (FontStyle.Regular));
            tb.SelectionColor = Color.Black;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.KeyValue != 219)
                return;
            GetInputChar(e.KeyValue);
            selectBox.TabIndex = 100;
            selectBox.Location = this.GetPositionFromCharIndex(this.SelectionStart);
            selectBox.Top += 10;
            selectBox.Show();
            selectBox.Focus();
        }
        /// <summary>
        /// 获取当前输入的符号
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        protected void GetInputChar(int keyValue)
        {
            byte[] kb_state = new byte[256];
            int[] charbuf = new int[4];

            if (GetKeyboardState(kb_state) > 0)
            {
                if (ToAscii(keyValue, keyValue, kb_state, charbuf, 2) > 0)
                {
                    inputChar = Convert.ToChar(charbuf[0]);

                }
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            selectBox.Hide();
            base.OnMouseClick(e);
        }
    }
}
